package com.cloudshop.service.mongo.service;

import com.cloudshop.service.common.utils.CheckEmptyUtil;
import com.cloudshop.service.mongo.dao.LogEntityMongoRepository;
import com.cloudshop.service.mongo.dto.GetLogByConditionParamsDto;
import com.cloudshop.service.mongo.entity.LogEntity;

import java.util.List;
import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

/**
 * Log Service.
 *
 * @author 宫新程
 * @since 2019/3/30 1:35
 */
@Service
public class LogService {

    @Resource
    private LogEntityMongoRepository logEntityMongoRepository;

    @Resource
    private MongoTemplate mongoTemplate;


    /**
     * 查询全部.
     *
     * @return 日志列表.
     */
    public List<LogEntity> findAll() {
        return logEntityMongoRepository.findAll();
    }

    /**
     * 清空请求日志表.
     */
    public void deleteAll() {
        logEntityMongoRepository.deleteAll();
    }

    /**
     * 多条件查询日志列表.
     */
    public Page<LogEntity> getLogByCondition(GetLogByConditionParamsDto paramsDto) {
        Sort sort = new Sort(Sort.Direction.DESC, "startTime");
        Pageable pageable = PageRequest.of(paramsDto.getPage() - 1, paramsDto.getRows(), sort);
        Page<LogEntity> result;
        if (CheckEmptyUtil.isNotEmpty(paramsDto.getRequestId())
                && CheckEmptyUtil.isNotEmpty(paramsDto.getHttpStatus())) {
            result =
                    logEntityMongoRepository.findByHttpStatusAndRequestId(
                            paramsDto.getHttpStatus(), paramsDto.getRequestId(), pageable);
        } else if (CheckEmptyUtil.isNotEmpty(paramsDto.getRequestId())) {
            result = logEntityMongoRepository.findByRequestId(paramsDto.getRequestId(), pageable);
        } else if (CheckEmptyUtil.isNotEmpty(paramsDto.getHttpStatus())) {
            result = logEntityMongoRepository.findByHttpStatus(paramsDto.getHttpStatus(), pageable);
        } else {
            result = logEntityMongoRepository.findAll(pageable);
        }
        return result;
    }

    /**
     * 多条件查询日志列表.
     */
    public List<LogEntity> search(GetLogByConditionParamsDto paramsDto) {
        Sort sort = new Sort(Sort.Direction.DESC, "startTime");
        Pageable pageable = PageRequest.of(paramsDto.getPage() - 1, paramsDto.getRows(), sort);
        Query query = new Query();
        Criteria criteria = new Criteria();
        if (CheckEmptyUtil.isNotEmpty(paramsDto.getRequestId())) {
            criteria.and("requestId").is(paramsDto.getRequestId());
        }
        if (CheckEmptyUtil.isNotEmpty(paramsDto.getHttpStatus())) {
            criteria.and("httpStatus").is(paramsDto.getHttpStatus());
        }
        query.addCriteria(criteria);
        return mongoTemplate.find(query.with(pageable), LogEntity.class);
    }
}
